package com.example.certificate.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.entity.SysActivite;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.SysActiviteMapper;
import com.example.certificate.service.SysActiviteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class SysActiviteServiceImpl extends ServiceImpl<SysActiviteMapper, SysActivite> implements SysActiviteService {

    @Resource
    private SysActiviteMapper sysActiviteMapper;


    @Override
    public ResultEntity insActivity(SysActivite model, User user) throws ParseException {

        model.setId(UUID.randomUUID().toString());
        model.setUserId(user.getId());

        int insResult = sysActiviteMapper.insert(model);
        if(insResult > 0)
            return new ResultEntity(ResultConstant.SUCCESS);

        return new ResultEntity(ResultConstant.MSG_CODE_ERROR);
    }


    @Override
    public ResultEntity delActivity(String id) {
        boolean insResult = removeById(id);
        if(insResult)
            return new ResultEntity(ResultConstant.SUCCESS);

        return new ResultEntity(ResultConstant.MSG_CODE_ERROR);
    }

    @Override
    public ResultEntity editActivity(SysActivite model) {
        int insResult = sysActiviteMapper.updateById(model);
        if(insResult > 0)
            return new ResultEntity(ResultConstant.SUCCESS);

        return new ResultEntity(ResultConstant.MSG_CODE_ERROR);
    }

    @Override
    public ResultEntity queryActivity() {
        List<SysActivite> activites = list(new QueryWrapper<SysActivite>());

        if(!CollectionUtils.isEmpty(activites)){

            for (SysActivite v:
                 activites) {

                v.setStartTime(v.getActiviteStartTime());
                v.setEndTime(v.getActiviteEndTime());
            }
        }
        return new ResultEntity(activites);
    }

    @Override
    public ResultEntity queryActivityPage(String name, Integer currentPage, Integer pageSize) {

        Page<SysActivite> page = new Page<>();
        page.setSize(pageSize == null ? 10 : pageSize);
        page.setCurrent(currentPage == null ? 1 : currentPage);

        Page<SysActivite> activitePage;

        if(StringUtils.hasLength(name)){
            activitePage = sysActiviteMapper.selectPage(page, new QueryWrapper<SysActivite>().like("activite_name", name));
        }else {
            activitePage = sysActiviteMapper.selectPage(page, null);
        }

        if(!CollectionUtils.isEmpty(activitePage.getRecords())){

            for (SysActivite v:
                    activitePage.getRecords()) {

                v.setStartTime(v.getActiviteStartTime());
                v.setEndTime(v.getActiviteEndTime());

                LocalDateTime currentTime = LocalDateTime.now();

                if(currentTime.isBefore(v.getStartTime())){
                    v.setCStatus(0);
                }else if(currentTime.isAfter(v.getStartTime()) && currentTime.isBefore(v.getEndTime())){
                    v.setCStatus(1);
                }else if(isActivityEnded(v.getEndTime())){
                    v.setCStatus(2);
                }
            }
        }

        return new ResultEntity(activitePage);
    }

    public  boolean isActivityEnded(LocalDateTime endTime) {
        LocalDateTime currentTime = LocalDateTime.now();
        return currentTime.isAfter(endTime);
    }
}
